package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Romell
 */
public class AlumnoDAO {
        Connection connection = null;
        PreparedStatement ptmt = null;
        ResultSet resultSet = null;

        public AlumnoDAO() {

        }

        private Connection getConnection() throws SQLException {
                Connection conn;
                conn = ConnectionFactory.getInstance().getConnection();
                return conn;
        }

//        public void add(AlumnoBean alumnoBean) {
//                try {
//                        String queryString = "INSERT INTO alumno(Edad, Nombre, Facultad, Escuela) VALUES(?,?,?,?)";
//                        connection = getConnection();
//                        ptmt = connection.prepareStatement(queryString);
//                        ptmt.setInt(1, alumnoBean.getEdad());
//                        ptmt.setString(2, alumnoBean.getNombre());
//                        ptmt.setString(3, alumnoBean.getFacultad());
//                        ptmt.setString(4, alumnoBean.getEscuela());
//                        ptmt.executeUpdate();
//                        System.out.println("Data alumno agregado");
//                } catch (SQLException e) {
//                        e.printStackTrace();
//                } finally {
//                        try {
//                                if (ptmt != null)
//                                        ptmt.close();
//                                if (connection != null)
//                                        connection.close();
//                        } catch (SQLException e) {
//                                e.printStackTrace();
//                        } catch (Exception e) {
//                                e.printStackTrace();
//                        }
//
//                }
//
//        }

          public void update(AlumnoBean alumnoBean) {

                try {
                        String queryString = "UPDATE alumno SET realizacion=? WHERE idAlumno=?";
                        connection = getConnection();
                        ptmt = connection.prepareStatement(queryString);
                        java.sql.Timestamp momentoTimestamp = new java.sql.Timestamp(alumnoBean.getRealizacion().getTime());
                        ptmt.setTimestamp(1, momentoTimestamp);
                        ptmt.setInt(2, alumnoBean.getIdAlumno());
                        ptmt.executeUpdate();
                        System.out.println("Tabla actualizada");
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (ptmt != null)
                                        ptmt.close();
                                if (connection != null)
                                        connection.close();
                        }

                        catch (SQLException e) {
                                e.printStackTrace();
                        } catch (Exception e) {
                                e.printStackTrace();

                        }
                }
        }


        public AlumnoBean findAlumno(String id, String pass) {
            AlumnoBean alumno = null;
                try {
                    // cambiar para que chape un alumno
                        String queryString = "SELECT * FROM alumno where idAlumno = "+id
                                +" and pass = "+pass;
                        connection = getConnection();
                        ptmt = connection.prepareStatement(queryString);
                        resultSet = getConnection().prepareStatement(queryString).executeQuery();
                        resultSet.next();
                        alumno = new AlumnoBean();
                        alumno.setApellidoM(resultSet.getString("apellidoM"));
                        alumno.setApellidoP(resultSet.getString("apellidoP"));
                        alumno.setCiclo(resultSet.getInt("ciclo"));
                        alumno.setEAP(resultSet.getString("EAP"));
                        alumno.setFacultad(resultSet.getString("Facultad"));
                        alumno.setIdAlumno(resultSet.getInt("idAlumno"));
                        alumno.setNombre(resultSet.getString("nombre"));
                        alumno.setRealizacion(resultSet.getDate("realizacion"));
                        
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (resultSet != null)
                                        resultSet.close();
                                if (ptmt != null)
                                        ptmt.close();
                                if (connection != null)
                                        connection.close();
                        } catch (SQLException e) {
                                e.printStackTrace();
                        } catch (Exception e) {
                                e.printStackTrace();
                        }

                }
                return alumno;
        }
}
